{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Mailサーバ(smtp,imap,LDAP連携)\n",
    "\n",
    "## dockerhostの設定"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "%%bash\n",
    "### メールクライアントのインストール\n",
    "sudo yum install -y mailx fetchmail procmail"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## コンテナの作成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "REPOSITORY                    TAG                      IMAGE ID            CREATED             SIZE\n",
      "ftakao2007/mail_centos7       virtual_for_jorurimail   35e0eebeeb79        18 hours ago        657MB\n",
      "ftakao2007/mail_centos7       latest                   0733ee002c86        19 hours ago        656MB\n",
      "ftakao2007/openldap_centos7   latest                   572b38993fa4        2 days ago          596MB\n",
      "ftakao2007/mysql5.6_centos7   latest                   09a773ad8e34        4 days ago          2.16GB\n",
      "ftakao2007/centos7            latest                   0fede72fa1fe        4 days ago          488MB\n",
      "CONTAINER ID        IMAGE                                COMMAND                  CREATED             STATUS              PORTS                                          NAMES\n",
      "3bc5d977b203        ftakao2007/openldap_centos7:latest   \"sh -c 'exec /usr/...\"   4 minutes ago       Up 4 minutes        0.0.0.0:389->389/tcp, 0.0.0.0:2223->22/tcp     openldap_centos7\n",
      "4c0ae849ad6a        ftakao2007/mysql5.6_centos7:latest   \"sh -c 'exec /usr/...\"   6 minutes ago       Up 6 minutes        0.0.0.0:3306->3306/tcp, 0.0.0.0:2222->22/tcp   mysql5.6_centos7\n",
      "REPOSITORY                    TAG                      IMAGE ID            CREATED             SIZE\n",
      "ftakao2007/mail_centos7       virtual_for_jorurimail   35e0eebeeb79        18 hours ago        657MB\n",
      "ftakao2007/mail_centos7       latest                   0733ee002c86        19 hours ago        656MB\n",
      "ftakao2007/openldap_centos7   latest                   572b38993fa4        2 days ago          596MB\n",
      "ftakao2007/mysql5.6_centos7   latest                   09a773ad8e34        4 days ago          2.16GB\n",
      "ftakao2007/centos7            latest                   0fede72fa1fe        4 days ago          488MB\n",
      "CONTAINER ID        IMAGE                                COMMAND                  CREATED             STATUS                  PORTS                                                               NAMES\n",
      "8784dfcb21c3        ftakao2007/mail_centos7:latest       \"sh -c 'exec /usr/...\"   1 second ago        Up Less than a second   0.0.0.0:2224->22/tcp, 0.0.0.0:2525->25/tcp, 0.0.0.0:1143->143/tcp   mail_server_centos7\n",
      "3bc5d977b203        ftakao2007/openldap_centos7:latest   \"sh -c 'exec /usr/...\"   4 minutes ago       Up 4 minutes            0.0.0.0:389->389/tcp, 0.0.0.0:2223->22/tcp                          openldap_centos7\n",
      "4c0ae849ad6a        ftakao2007/mysql5.6_centos7:latest   \"sh -c 'exec /usr/...\"   6 minutes ago       Up 6 minutes            0.0.0.0:3306->3306/tcp, 0.0.0.0:2222->22/tcp                        mysql5.6_centos7\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Creating mail_server_centos7\n"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "### 変数の設定\n",
    "container=\"mail_server_centos7\"\n",
    "docker_compose_file=\"docker-compose_${container}.yml\"\n",
    "smtp_port=\"2525\"\n",
    "imap_port=\"1143\"\n",
    "ssh_port=\"2224\"\n",
    "\n",
    "### イメージ、コンテナが無い状態の確認\n",
    "docker images\n",
    "docker ps -a\n",
    "\n",
    "### docker-composeディレクトリに移動\n",
    "#work_dir=\"/home/jsd/work/build/docker-compose\"\n",
    "work_dir=\"${PYENV_DIR}/work/build/docker-compose\"\n",
    "cd ${work_dir}\n",
    "\n",
    "### docker-composeファイルの作成\n",
    "cat <<- EOS > ${docker_compose_file}\n",
    "${container}:\n",
    "   image: ftakao2007/mail_centos7:latest\n",
    "   container_name: ${container}\n",
    "   privileged: true\n",
    "   tty: true\n",
    "   ports:\n",
    "     - \"${smtp_port}:25\"\n",
    "     - \"${imap_port}:143\"\n",
    "     - \"${ssh_port}:22\"\n",
    "   #command: sh -c '/sbin/init'\n",
    "   command: sh -c 'exec /usr/sbin/init && systemctl restart sshd.service && systemctl restart postfix.service && systemctl restart dovecot.service'\n",
    "EOS\n",
    "\n",
    "### コンテナの作成\n",
    "docker-compose -f ${docker_compose_file} up -d\n",
    "#sleep 80\n",
    "\n",
    "### イメージ、コンテナの確認\n",
    "docker images\n",
    "docker ps -a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "### メールの送信テスト\n",
    "#mail_server=\"192.168.33.11\"\n",
    "#mail_server=\"10.136.3.168\"\n",
    "mail_server=\"127.0.0.1\"\n",
    "smtp_port=\"2525\"\n",
    "imap_port=\"1143\"\n",
    "mail_from=\"joruri_test12@dockernet.co.jp\"\n",
    "mail_to=\"joruri_test11@dockernet.co.jp\"\n",
    "\n",
    "echo \"mail body\" | mail -s \"mail title\" -S \"smtp=smtp://${mail_server}:${smtp_port}\" -r ${mail_from} ${mail_to}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3 messages for 0050011 at 127.0.0.1.\n"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "### メールの受信テスト\n",
    "#mail_server=\"192.168.33.11\"\n",
    "#mail_server=\"10.136.3.168\"\n",
    "mail_server=\"127.0.0.1\"\n",
    "imap_port=\"1143\"\n",
    "\n",
    "### procmailの設定\n",
    "cat <<- EOS > ~/.fetchmailrc\n",
    "poll ${mail_server}\n",
    "  protocol imap\n",
    "  port ${imap_port}\n",
    "  username \"0050011\"\n",
    "  password \"pass\"\n",
    "  mda \"/usr/local/bin/procmail\"\n",
    "EOS\n",
    "chmod 700 ~/.fetchmailrc\n",
    "\n",
    "### postfixの設定変更。メールサーバを既存で使ってる場合は設定に注意\n",
    "sudo sed -i -e 's/#mailbox_command = \\/some\\/where\\/procmail$/mailbox_command = \\/usr\\/bin\\/procmail/' /etc/postfix/main.cf\n",
    "\n",
    "### メールが届いていることの確認\n",
    "fetchmail -c"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## VIPの付与"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500\n",
      "        inet 172.17.0.4  netmask 255.255.0.0  broadcast 0.0.0.0\n",
      "        ether 02:42:ac:11:00:04  txqueuelen 0  (Ethernet)\n",
      "        RX packets 173  bytes 15531 (15.1 KiB)\n",
      "        RX errors 0  dropped 0  overruns 0  frame 0\n",
      "        TX packets 202  bytes 19096 (18.6 KiB)\n",
      "        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0\n",
      "\n",
      "eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500\n",
      "        inet 172.17.33.13  netmask 255.255.0.0  broadcast 172.17.255.255\n",
      "        ether 02:42:ac:11:00:04  txqueuelen 0  (Ethernet)\n",
      "\n",
      "lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536\n",
      "        inet 127.0.0.1  netmask 255.0.0.0\n",
      "        loop  txqueuelen 1  (Local Loopback)\n",
      "        RX packets 0  bytes 0 (0.0 B)\n",
      "        RX errors 0  dropped 0  overruns 0  frame 0\n",
      "        TX packets 0  bytes 0 (0.0 B)\n",
      "        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "### ネットワーク情報の確認\n",
    "container=\"mail_server_centos7\"\n",
    "docker exec ${container} ifconfig -a "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500\n",
      "        inet 172.17.0.4  netmask 255.255.0.0  broadcast 0.0.0.0\n",
      "        ether 02:42:ac:11:00:04  txqueuelen 0  (Ethernet)\n",
      "        RX packets 345  bytes 33111 (32.3 KiB)\n",
      "        RX errors 0  dropped 0  overruns 0  frame 0\n",
      "        TX packets 410  bytes 41383 (40.4 KiB)\n",
      "        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0\n",
      "\n",
      "eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500\n",
      "        inet 172.17.33.13  netmask 255.255.0.0  broadcast 172.17.255.255\n",
      "        ether 02:42:ac:11:00:04  txqueuelen 0  (Ethernet)\n",
      "\n",
      "lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536\n",
      "        inet 127.0.0.1  netmask 255.0.0.0\n",
      "        loop  txqueuelen 1  (Local Loopback)\n",
      "        RX packets 10  bytes 707 (707.0 B)\n",
      "        RX errors 0  dropped 0  overruns 0  frame 0\n",
      "        TX packets 10  bytes 707 (707.0 B)\n",
      "        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0\n",
      "\n",
      "6 messages for 0050011 at 172.17.33.13.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "bash: line 28: sleep1: command not found\n"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "### VIPを付与する\n",
    "container=\"mail_server_centos7\"\n",
    "imap_port=\"143\"\n",
    "smtp_port=\"25\"\n",
    "mail_from=\"joruri_test12@dockernet.co.jp\"\n",
    "mail_to=\"joruri_test11@dockernet.co.jp\"\n",
    "ip=\"172.17.33.13\"\n",
    "netmask=\"255.255.0.0\"\n",
    "device=\"eth0\"\n",
    "docker exec ${container} ifconfig ${device}:1 ${ip} netmask ${netmask}\n",
    "\n",
    "### ネットワーク確認\n",
    "docker exec ${container} ifconfig -a\n",
    "\n",
    "### procmailの設定\n",
    "cat <<- EOS > ~/.fetchmailrc\n",
    "poll ${ip}\n",
    "  protocol imap\n",
    "  port ${imap_port}\n",
    "  username \"0050011\"\n",
    "  password \"pass\"\n",
    "  mda \"/usr/local/bin/procmail\"\n",
    "EOS\n",
    "chmod 700 ~/.fetchmailrc\n",
    "\n",
    "### メールの送信確認\n",
    "echo \"mail body\" | mail -s \"mail title\" -S \"smtp=smtp://${ip}:${smtp_port}\" -r ${mail_from} ${mail_to}\n",
    "sleep1\n",
    "\n",
    "### メールの受信確認\n",
    "fetchmail -c"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Mailサーバ(smtp,imap,LDAP連携,JoruriMail v2用virtualプラグイン設定)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## コンテナの作成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "REPOSITORY                    TAG                      IMAGE ID            CREATED             SIZE\n",
      "ftakao2007/mail_centos7       virtual_for_jorurimail   35e0eebeeb79        18 hours ago        657MB\n",
      "ftakao2007/mail_centos7       latest                   0733ee002c86        19 hours ago        656MB\n",
      "ftakao2007/openldap_centos7   latest                   572b38993fa4        2 days ago          596MB\n",
      "ftakao2007/mysql5.6_centos7   latest                   09a773ad8e34        4 days ago          2.16GB\n",
      "ftakao2007/centos7            latest                   0fede72fa1fe        4 days ago          488MB\n",
      "CONTAINER ID        IMAGE                                COMMAND                  CREATED             STATUS              PORTS                                                               NAMES\n",
      "8784dfcb21c3        ftakao2007/mail_centos7:latest       \"sh -c 'exec /usr/...\"   16 minutes ago      Up 16 minutes       0.0.0.0:2224->22/tcp, 0.0.0.0:2525->25/tcp, 0.0.0.0:1143->143/tcp   mail_server_centos7\n",
      "3bc5d977b203        ftakao2007/openldap_centos7:latest   \"sh -c 'exec /usr/...\"   20 minutes ago      Up 20 minutes       0.0.0.0:389->389/tcp, 0.0.0.0:2223->22/tcp                          openldap_centos7\n",
      "4c0ae849ad6a        ftakao2007/mysql5.6_centos7:latest   \"sh -c 'exec /usr/...\"   22 minutes ago      Up 22 minutes       0.0.0.0:3306->3306/tcp, 0.0.0.0:2222->22/tcp                        mysql5.6_centos7\n",
      "REPOSITORY                    TAG                      IMAGE ID            CREATED             SIZE\n",
      "ftakao2007/mail_centos7       virtual_for_jorurimail   35e0eebeeb79        18 hours ago        657MB\n",
      "ftakao2007/mail_centos7       latest                   0733ee002c86        19 hours ago        656MB\n",
      "ftakao2007/openldap_centos7   latest                   572b38993fa4        2 days ago          596MB\n",
      "ftakao2007/mysql5.6_centos7   latest                   09a773ad8e34        4 days ago          2.16GB\n",
      "ftakao2007/centos7            latest                   0fede72fa1fe        4 days ago          488MB\n",
      "CONTAINER ID        IMAGE                                            COMMAND                  CREATED             STATUS                  PORTS                                                               NAMES\n",
      "d466248a93d9        ftakao2007/mail_centos7:virtual_for_jorurimail   \"sh -c 'exec /usr/...\"   3 seconds ago       Up Less than a second   0.0.0.0:2225->22/tcp, 0.0.0.0:2526->25/tcp, 0.0.0.0:1144->143/tcp   mail_server_virtual_for_jorurimail_centos7\n",
      "8784dfcb21c3        ftakao2007/mail_centos7:latest                   \"sh -c 'exec /usr/...\"   16 minutes ago      Up 16 minutes           0.0.0.0:2224->22/tcp, 0.0.0.0:2525->25/tcp, 0.0.0.0:1143->143/tcp   mail_server_centos7\n",
      "3bc5d977b203        ftakao2007/openldap_centos7:latest               \"sh -c 'exec /usr/...\"   20 minutes ago      Up 20 minutes           0.0.0.0:389->389/tcp, 0.0.0.0:2223->22/tcp                          openldap_centos7\n",
      "4c0ae849ad6a        ftakao2007/mysql5.6_centos7:latest               \"sh -c 'exec /usr/...\"   22 minutes ago      Up 22 minutes           0.0.0.0:3306->3306/tcp, 0.0.0.0:2222->22/tcp                        mysql5.6_centos7\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Creating mail_server_virtual_for_jorurimail_centos7\n"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "### 変数の設定\n",
    "container=\"mail_server_virtual_for_jorurimail_centos7\"\n",
    "docker_compose_file=\"docker-compose_${container}.yml\"\n",
    "smtp_port=\"2526\"\n",
    "imap_port=\"1144\"\n",
    "ssh_port=\"2225\"\n",
    "\n",
    "### イメージ、コンテナが無い状態の確認\n",
    "docker images\n",
    "docker ps -a\n",
    "\n",
    "### docker-composeディレクトリに移動\n",
    "#work_dir=\"/home/jsd/work/build/docker-compose\"\n",
    "work_dir=\"${PYENV_DIR}/work/build/docker-compose\"\n",
    "cd ${work_dir}\n",
    "\n",
    "### docker-composeファイルの作成\n",
    "cat <<- EOS > ${docker_compose_file}\n",
    "${container}:\n",
    "   image: ftakao2007/mail_centos7:virtual_for_jorurimail\n",
    "   container_name: ${container}\n",
    "   privileged: true\n",
    "   tty: true\n",
    "   ports:\n",
    "     - \"${smtp_port}:25\"\n",
    "     - \"${imap_port}:143\"\n",
    "     - \"${ssh_port}:22\"\n",
    "   #command: sh -c '/sbin/init'\n",
    "   command: sh -c 'exec /usr/sbin/init && systemctl restart sshd.service && systemctl restart postfix.service && systemctl restart dovecot.service'\n",
    "EOS\n",
    "\n",
    "### コンテナの作成\n",
    "docker-compose -f ${docker_compose_file} up -d\n",
    "#sleep 80\n",
    "\n",
    "### イメージ、コンテナの確認\n",
    "docker images\n",
    "docker ps -a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## VIPの付与"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500\n",
      "        inet 172.17.0.5  netmask 255.255.0.0  broadcast 0.0.0.0\n",
      "        ether 02:42:ac:11:00:05  txqueuelen 0  (Ethernet)\n",
      "        RX packets 8  bytes 648 (648.0 B)\n",
      "        RX errors 0  dropped 0  overruns 0  frame 0\n",
      "        TX packets 0  bytes 0 (0.0 B)\n",
      "        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0\n",
      "\n",
      "lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536\n",
      "        inet 127.0.0.1  netmask 255.0.0.0\n",
      "        loop  txqueuelen 1  (Local Loopback)\n",
      "        RX packets 0  bytes 0 (0.0 B)\n",
      "        RX errors 0  dropped 0  overruns 0  frame 0\n",
      "        TX packets 0  bytes 0 (0.0 B)\n",
      "        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "### ネットワーク情報の確認\n",
    "container=\"mail_server_virtual_for_jorurimail_centos7\"\n",
    "docker exec ${container} ifconfig -a "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500\n",
      "        inet 172.17.0.5  netmask 255.255.0.0  broadcast 0.0.0.0\n",
      "        ether 02:42:ac:11:00:05  txqueuelen 0  (Ethernet)\n",
      "        RX packets 158  bytes 15887 (15.5 KiB)\n",
      "        RX errors 0  dropped 0  overruns 0  frame 0\n",
      "        TX packets 191  bytes 19771 (19.3 KiB)\n",
      "        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0\n",
      "\n",
      "eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500\n",
      "        inet 172.17.33.14  netmask 255.255.0.0  broadcast 172.17.255.255\n",
      "        ether 02:42:ac:11:00:05  txqueuelen 0  (Ethernet)\n",
      "\n",
      "lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536\n",
      "        inet 127.0.0.1  netmask 255.0.0.0\n",
      "        loop  txqueuelen 1  (Local Loopback)\n",
      "        RX packets 0  bytes 0 (0.0 B)\n",
      "        RX errors 0  dropped 0  overruns 0  frame 0\n",
      "        TX packets 0  bytes 0 (0.0 B)\n",
      "        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0\n",
      "\n",
      "5 messages for 0050011 at 172.17.33.14.\n"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "### VIPを付与する\n",
    "container=\"mail_server_virtual_for_jorurimail_centos7\"\n",
    "imap_port=\"143\"\n",
    "smtp_port=\"25\"\n",
    "mail_from=\"joruri_test12@dockernet.co.jp\"\n",
    "mail_to=\"joruri_test11@dockernet.co.jp\"\n",
    "ip=\"172.17.33.14\"\n",
    "netmask=\"255.255.0.0\"\n",
    "device=\"eth0\"\n",
    "docker exec ${container} ifconfig ${device}:1 ${ip} netmask ${netmask}\n",
    "\n",
    "### ネットワーク確認\n",
    "docker exec ${container} ifconfig -a\n",
    "\n",
    "### procmailの設定\n",
    "cat <<- EOS > ~/.fetchmailrc\n",
    "poll ${ip}\n",
    "  protocol imap\n",
    "  port ${imap_port}\n",
    "  username \"0050011\"\n",
    "  password \"pass\"\n",
    "  mda \"/usr/local/bin/procmail\"\n",
    "EOS\n",
    "chmod 700 ~/.fetchmailrc\n",
    "\n",
    "### メールの送信確認\n",
    "echo \"mail body\" | mail -s \"mail title\" -S \"smtp=smtp://${ip}:${smtp_port}\" -r ${mail_from} ${mail_to}\n",
    "sleep 1\n",
    "\n",
    "### メールの受信確認\n",
    "fetchmail -c"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
